package com.myschool.server.manager;

import org.myschool.common.exception.ApplicationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.myschool.server.dao.support.BaseEntityDaoHibernate;
import com.myschool.server.model.BaseEntity;

@Service
@Transactional
public abstract class BaseManager<T extends BaseEntity> implements Manager<T> {

	/*
	 * @Autowired
	 * 
	 * @Qualifier("baseDao") private BaseEntityDaoHibernate dao;
	 */

	protected abstract BaseEntityDaoHibernate getDao();

	@Override
	@Transactional(readOnly = false)
	public T create(T entity) throws ApplicationException {

		return (T) getDao().save(entity);
	}

	@Override
	@Transactional(readOnly = false)
	public T update(T entity) throws ApplicationException {

		return (T) getDao().save(entity);
	}

	@Override
	@Transactional(readOnly = true)
	public T retrieve(T entity) throws ApplicationException {
		return (T) getDao().get(entity.getEntityId());
	}
	@Override
	@Transactional(readOnly = true)
	public T retrieve(Long entityId) throws ApplicationException {
		return (T) getDao().get(entityId);
	}

	@Override
	@Transactional(readOnly = false)
	public void delete(T entity) throws ApplicationException {
		getDao().remove(entity.getEntityId());

	}
	@Override
	@Transactional(readOnly = false)
	public void delete(Long entityId) throws ApplicationException {
		getDao().remove(entityId);

	}

}
